<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--创建字典数据模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-seller">
    <div class="modal-dialog" style="width:500px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">卖方信息</h4>
            </div>
            <div class="modal-body">
                <form id="sellerForm" class="form-horizontal">
                    <input type="hidden" id="id" name="id">
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="sellerForm-name">卖方联系人</label>
                        <div class="col-sm-6">
                            <input id="sellerForm-name" type="text" class="form-control" name="sellerName"
                                   placeholder="请输入名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="sellerForm-sellerPhone">卖方电话号</label>
                        <div class="col-sm-6">
                            <input id="sellerForm-sellerPhone" type="number" class="form-control" name="sellerPhone"
                                   placeholder="请输入电话号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="sellerForm-sellerEmail">卖方邮箱</label>
                        <div class="col-sm-6">
                            <input id="sellerForm-sellerEmail" type="email" class="form-control" name="sellerEmail"
                                   placeholder="请输入邮箱">
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info"
                        id="btn-create-seller">提交
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>卖方信息</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-sellerName">联系人</label>
                                    <input id="query-sellerName" type="text" name="sellerName" class="form-control"
                                           placeholder="请输入卖家联系人">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                                <a href="javascript:void(0);"
                                   class="btn btn-info pull-right" onclick="openCreateVerifyModal()">
                                    <span class="Bold">添加</span>
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /** 打开创建数据模态框 */
    function openCreateVerifyModal() {
        js.modal.open("modal-seller");
    }

    /** 打开编辑数据模态框 */
    function openEditVerifyModal(result) {
        $("#sellerForm").fillData(result);
        js.modal.open("modal-seller");
        setTimeout(function () {
            js.validate.validing("sellerForm");
        }, 500);
    }


    $(function () {

        /** 加载列表 */
        table = js.table.init({
            url: ctx + "sellerList",
            showExport: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: "卖方联系人", field: 'sellerName'},
                {title: "卖方电话", field: 'sellerPhone'},
                {title: "卖方邮箱", field: 'sellerEmail'},
                {title: "id", field: 'id', visible: false},
                {
                    title: "操作",
                    width: '250',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="openEditVerifyModal(' + js.jsonStringify(row) + ')"> 编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="deleteSeller(' + js.jsonStringify(row) + ')"> 删除</a> ');
                        return actions.join('');
                    }
                }
            ]
        });


        /** 创建或编辑 */
        $("#btn-create-seller").click(function () {
            var url = ctx + "sellerSave";
            if ($("#id").val()) {
                url = ctx + "sellerUpdate";
            }
            js.validSubmit({
                formId: "sellerForm",
                url: url,
                data: new FormData($("#sellerForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-seller");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });

        /** 初始化验证 */
        js.validate.init("sellerForm", {
            fields: {
                sellerName: {
                    validators: {
                        notEmpty: {message: '请输入卖家姓名'},

                    }
                },
                sellerPhone: {
                    validators: {
                        notEmpty: {message: '请输入卖家电话号'},

                    }
                },
                sellerEmail: {
                    validators: {
                        notEmpty: {message: '请输入卖家邮箱'},

                    }
                }
            }
        });

        /** 隐藏 modal-create-verify */
        js.modal.hideEvent("modal-seller", function () {
            js.validate.reset("sellerForm");
            js.reset("sellerForm");
        })
    });


    function deleteSeller(row) {
        js.modal.confirm("确认删除吗？", function (result) {
            if (result) {
                callBack();
            }
            $.ajax({
                type: 'GET',
                url: ctx + "sellerDelete?id=" + row.id,
                processData: false,
                contentType: false,
                success: function (msg) {
                    if (msg) {
                        js.modal.success("删除成功");
                        $("#bootstrap-table").bootstrapTable('refresh');
                        js.modal.closeLoading();
                    } else {
                        js.modal.warning(msg);
                    }
                }
            });
        })
    }

</script>
</body>
</html>
